package com.mycompany.lctwitter.persist.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.mycompany.lctwitter.persist.model.Location;
import com.mycompany.lctwitter.persist.model.Message;
import com.mycompany.lctwitter.persist.model.User;

@Repository("messageDao")
public class MessageDao extends HibernateDao<Message, Long> {

    public void add(long userId, String message, long locationId) {
        Message entity = new Message();
        User user = new User(userId);
        Location location = new Location(locationId);
        entity.setLocation(location);
        entity.setUser(user);
        entity.setMessage(message);
        entity.setCreatedTime(new Date());
        save(entity);
    }

    public List<Message> listByUser(long userId) {
        return find("from Message where user.id=? order by createdTime desc", new Object[] { userId });
    }

    public List<Message> listByUserAndLocation(long userId, long locationId) {
        return find("from Message where user.id=? and location.id=? order by createdTime desc", new Object[] { userId, locationId });
    }

    public List<Message> listByLocation(long locationId) {
        return find("from Message where location.id=? order by createdTime desc", new Object[] { locationId });

    }

}
